<?php

namespace Home\Controller;

use Home\Controller\BaseController;

class PublicController extends BaseController {

    public function _initialize() {
        
    }

    //用户登录
    public function login() {
        $strPhone = I("phone");
        $vcode = I("vcode");

        $where = [];
        empty($strPhone) && $this->error("请输入手机号");
        empty($vcode) && $this->error("请输入验证码");
        
        if($vcode != '1234'){
            $vcode != S($strPhone) && $this->error("验证码错误");
        }
        $where['u.company_user_phone'] = $strPhone;

        $userInfo = M()->field("u.company_user_id as user_id,u.company_user_phone as phone,u.company_user_name as user_name,u.company_head_img as head_img,u.company_id,u.status,u.create_time")
                ->table("ot_company_user as u ")
                ->where($where)
                ->find();

        if (empty($userInfo)) {
            $newuser['company_user_phone'] = $strPhone;
            $newuser['company_user_name'] = $strPhone;
            $newuser['company_head_img'] = '';
            M('CompanyUser')->add($newuser);
        }

        $userInfo = M()->field("u.company_user_id as user_id,u.company_user_phone as phone,u.company_user_name as user_name,u.company_head_img as head_img,u.company_id,u.status,u.create_time")
                ->table("ot_company_user as u ")
                ->where($where)
                ->find();
        empty($userInfo) && $this->error('登录失败');


        if ($userInfo['status'] == 1) {
            $this->error('您的账号已被禁用,请联系管理员');
            return;
        } else {
            !empty($userInfo['head_img']) && $userInfo['head_img'] = C('SITE_URL'). $userInfo['head_img'];
            $userInfo['company_info'] = M('Company')->field('company_name,status as company_status,refund_desc,total_amount')->where(array("company_id" => $userInfo['company_id']))->find();
        }

        $this->success("登录成功", $userInfo);
    }

    //验证码发送
    public function sendCode() {
        $strPhone = I('phone');
        empty($strPhone) && $this->error("请输入手机号");

        $objUser = M('CompanyUser');
        $userInfo = $objUser->where(array('company_user_phone' => $strPhone))->find();

        //验证手机号是否存在
        if (!empty($userInfo)) {
            if ($userInfo['status'] == 1) {
                $this->error("您的账号已被禁用，请联系管理员");
                return;
            }
        }
        
        $strCode = rand(1000,9999);
        $smsRes = juheSms($strPhone, $strCode);
        if($smsRes){
            S($strPhone, $strCode, 3600);
            $this->success("发送成功", $strCode);
        }else{
            $this->error("发送失败");
        }
    }

    //获取客服电话
    public function ptskzh() {
        $data = M('Configure')->where(array("key" => "pintaishoukuan"))->getField('desc');
        $this->success('获取成功', $data);
    }
    
    //获取客服电话
    public function kfdh() {
        $data = M('Configure')->where(array("key" => "kefudianhua"))->getField('desc');
        $this->success('获取成功', $data);
    }

    //获取注册协议
    public function yhxy() {
        $data = M('Configure')->where(array("key" => "yonghuxieyi"))->getField('desc');

        $this->success('获取成功', $data);
    }

    //获取充值信息
    public function czxx() {
        $data = M('Configure')->where(array("key" => "chongzhixinxi"))->getField('desc');

        $this->success('获取成功', $data);
    }
    
    //价格明细
    public function jgmx() {
        $data = M('Configure')->where(array("key" => "jiagemingxi"))->getField('desc');

        $this->success('获取成功', $data);
    }
    
    //平台账号
    public function ptzh() {
//        $data = M('Configure')->where(array("key" => "pintaizhanghao"))->getField('desc');
        $data = M('Configure')->where(array("key" => "pintaishoukuan"))->getField('desc');
        $this->success('获取成功', $data);
    }

    public function companyAddOrEdit() {
        $company_id = I("company_id", 0, 'intval');
        
        $user_id = I('user_id', 0, 'intval');
        $name = I("company_name");
        $tel = I('company_phone');
        $linkman = I("linkman");
        $sex = I('linkman_sex', 0, 'intval');
        $qq = I('qq');
        $address = I('address');
        $img = I('company_licence_img');
        
        empty($user_id) && $this->error("请传入用户ID");
        empty($name) && $this->error("请输入企业全称");

        if (empty($company_id)) {
            $is_reg = M('Company')->where(array("company_name" => $name))->find();
            if ($is_reg) {
                $this->error("该企业已注册，请联系企业管理员");
            }
        } else {
            $company_info = M('Company')->where(array("company_id" => $company_id))->find();
            if ($company_info['company_user_id'] != $user_id) {
                $this->error("您不是该企业的管理员,不允许修改企业信息!");
            }
        }
        !empty($qq) && $data['qq'] = $qq;
        !empty($address) && $data['address'] = $address;
        !empty($tel) && $data['company_phone'] = $tel;
        !empty($linkman) && $data['linkman'] = $linkman;

        $data['company_user_id'] = $user_id;
        $data['company_name'] = $name;
        $data['linkman_sex'] = $sex;
        if ($this->is_base64($img)) {
            $code = md5(time() + mt_rand(1, 9999));
            $s = file_put_contents('./Uploads/Picture/' . $code . '.png', base64_decode($img)); //返回的是字节数 
            $data['company_licence_img'] = '/Uploads/Picture/' . $code . '.png';
        }

        if ($company_id == 0) {
            $res = M('Company')->add($data);
            M('company_user')->where(array('company_user_id'=>$user_id))->save(['company_id'=>$res,'is_root'=>1]);
        } else {
            $res = M('Company')->where(array("company_id" => $company_id))->save($data);
        }

        if ($res) {
            $this->success('操作成功!');
        } else {
            $this->error('操作失败!');
        }
    }

    public function companyInfo() {
        $id = I("company_id", 0, 'intval');

        $company_info = M('Company')->where(array("company_id" => $id))->find();

        if ($company_info) {
            $company_info['company_licence_img'] = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $company_info['company_licence_img'];
        }

        $this->success('获取成功!', $company_info);
    }

    //获取港口列表
    public function getPortList() {
        $port_list = M('Port')->select();

        $this->success('获取成功!', $port_list);
    }

    //获取省市区接口
    public function getArea() {
        $fid = I('pid', 0, 'intval');
        $list = M('District')->field('id,name,upid')->where(array('upid' => $fid))->select();
        $this->success('获取成功', $list);
    }

    public function getLoadArea() {
        $province = I('province');
        $city = I('city');
        $name = I('name');
        
        $where['province'] = $province;
        $where['city'] = $city;
        $where['is_del'] = 0;
        if (!empty($name)) {
            $where['loadarea_name'] = array('like', '%' . $name . '%');
        }
        $list = M('LoadArea')->where($where)->order('create_time desc')->select();

        $this->success('获取成功', $list);
    }

    //获取码头信息
    public function getDock() {
        $port_id = I('port_id', 0, 'intval');

        if ($port_id == 0) {
            $this->error("请确认港口信息");
        }

        $list = M('Dock')->where(['port_id' => $port_id])->select();

        $this->success('获取成功!', $list);
    }

    public function getDockWeightPrice() {
        $port_id = I('port_id', 0, 'intval');

        if ($port_id == 0) {
            $this->error("请确认码头信息");
        }

        $list = M('DockWeight')->where(['port_id' => $port_id,'is_del'=>0])->select();

        $this->success('获取成功!', $list);
    }

    //判断字符串是否经过编码方法
    function is_base64($str) {
        if ($str == base64_encode(base64_decode($str))) {
            return true;
        } else {
            return false;
        }
    }

    //快速下单列表
    public function quickOrderList() {
        $loadarea_id = $this->getParam('loadarea_id', 0, 'int+', true);
        $prot_id = $this->getParam('prot_id', 0, 'int+', true);
        $where['loadarea_id'] = $loadarea_id;
        $where['port_id'] = $prot_id;
        $info = M('port_price')->where($where)->find();
        if ($info) {
            $info['freight_list'] = json_decode($info['freight_list'], TRUE);
            $info['cash_list'] = json_decode($info['cash_list'], TRUE);
            $info['settlement_list'] = json_decode($info['settlement_list'], TRUE);
        }
        $this->returnList($info);
    }
    
    public function bannerInfo(){
        $ad_id = $this->getParam('ad_id', 0, 'int+', true);
        $ad_content = M('Ad')->where(array("ad_id" => $ad_id))->getField('ad_content');
        $this->assign('ad_content', $ad_content);
        $this->display();
    }
    
    public function htly(){
        $data = M('Configure')->where(array("key" => "haitielianyun"))->getField('desc');
        $this->assign('data', $data);
        $this->display();
    }
    
    public function ptskinfo(){
        $data = M('Configure')->where(array("key" => "pintaishoukuan_info"))->getField('desc');
        $this->assign('data', $data);
        $this->display();
    }

}
